using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Winter.BackEnd.Models.Domain;
using Winter.BackEnd.Models.DataTypes;

namespace Winter.BackEnd.Controllers
{
    [Authorize]
    public class MessageController : ApplicationController
    {
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            return View(Repository.CurrentUser.Person.PersonInRole);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetRecievedMessages()
        {
            var result = new JsonResult()
            {
                Data = new { messages = Repository.CurrentUser.Person.PersonInRole.IncomeMessages }
            };
            return result;
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetSentMessages()
        {
            return Json(Repository.CurrentUser.Person.PersonInRole.SentMessages);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ViewMessage(int messageId)
        {
            ///TODO: brainfuck
            var message = Repository.CurrentDataContext.Messages
                .FirstOrDefault(m => m.ID == messageId && (m.SenderID == Repository.CurrentUser.Person.ID ||
                    m.PersonMessages.FirstOrDefault(mes => mes.MessageID == m.ID).PersonID == Repository.CurrentUser.Person.ID));

            if (message == null)
            {
                return RedirectToAction("Index");
            }

            message.IsNew = false;

            var result = new Winter.BackEnd.Models.DataTypes.MessageDTO()
                {
                    Id = message.ID,
                    Sender = Repository.CurrentDataContext.Persons.FirstOrDefault(p => p.ID == message.SenderID).FullName,
                    SenderId = message.SenderID,
                    SenderPhoto = Repository.CurrentDataContext.Persons.FirstOrDefault(p => p.ID == message.SenderID).Image == null ?
                            Server.MapPath(Constants.DefaultPhoto) : Repository.CurrentDataContext.Persons.FirstOrDefault(p => p.ID == message.SenderID).Image.ToString(),
                    Subject = message.Subject,
                    Time = message.SendTime.ToString("HH:mm dd.MM.yyyy"),
                    MessageBody = message.Body,
                    Recipient = Repository.CurrentUser.Person.FullName,
                    SenderName = message.Person.aspnet_User.UserName,
                    SenderID = message.Person.ID.ToString()
                };

            return Json(result);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public string SendMessage(MessageDTO message)
        {
            Person recipient = null;
            if (Repository.CurrentDataContext.Persons.Any(p => p.aspnet_User.UserName == message.Recipient))
            {
                recipient = Repository.CurrentDataContext.Persons.SingleOrDefault(p => p.aspnet_User.UserName == message.Recipient);

                Message messageTarget = new Message();

                messageTarget.Subject = message.Subject;
                messageTarget.Body = message.MessageBody;
                messageTarget.SendTime = DateTime.Now;
                messageTarget.IsNew = true;
                messageTarget.IsDeleted = false;
                messageTarget.PersonMessages.Add(new PersonMessage()
                {
                    PersonID = recipient.ID
                });

                Repository.CurrentUser.Person.Messages.Add(messageTarget);

                Repository.CurrentDataContext.SubmitChanges();
                return "Success";
            }
            return "Invalid recipient name.";
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult DeleteMessage(int messageID)
        {
            JsonResult result = new JsonResult();
            Message messageForDelete = Repository.GetIncomeMessageForCurrentUserByID(messageID);
            if (!Repository.GetIncomeMessageForCurrentUserByID(messageID).IsDeleted)
            {
                Repository.GetIncomeMessageForCurrentUserByID(messageID).IsDeleted = true;
                Repository.CurrentDataContext.SubmitChanges();
                result.Data = new { message = "Success" };
            }
            else
            {
                result.Data = new { message = "This message already deleted" };
            }
            return result;
        }
    }
}
